package com.compty.paint.web.member;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.AuditState;
import com.compty.paint.domain.Exhibition;
import com.compty.paint.domain.ImageInfo;
import com.compty.paint.domain.LogInfo;
import com.compty.paint.domain.ProvinceInfo;
import com.compty.paint.domain.UploadState;
import com.compty.paint.domain.support.LogTypeEnum;
import com.compty.paint.manager.AuditStateManager;
import com.compty.paint.manager.ExhibitionManager;
import com.compty.paint.manager.ImageInfoManager;
import com.compty.paint.manager.LogInfoManager;
import com.compty.paint.manager.ProvinceInfoManager;
import com.compty.paint.manager.UploadStateManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * 展览Action
 * @author zhuqh
 */
public class ExhibitionAction  extends BaseAction<Exhibition>{

	private static final long serialVersionUID = 1225228421224009261L;

	private ExhibitionManager exhibitionManager;
	private UploadStateManager uploadStateManager;
	private AuditStateManager auditStateManager;
	private ProvinceInfoManager provinceInfoManager;
	private LogInfoManager logInfoManager;
	private ImageInfoManager imageInfoManager;
	
	// ====================================================
	private List<AuditState> auditStateList;
	private List<ProvinceInfo> provinceInfoList;
	private Exhibition exhibition;
	private Date startDate;
	private Date endDate;
	private String operateStr; //1表示隐藏,2表示删除
	private String auditStateType; //查询审核类型
	
	/**
	 *@Todo:添加展览
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 下午02:31:17
	 */
	public String addPrepare(){
		
		this.exhibition = null;
		
		this.provinceInfoList = provinceInfoManager.getAll();
		
		return SUCCESS;
	}
	
	/**
	 *@Todo:添加全国展览
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 下午02:31:17
	 */
	public String addPrepareAddition(){
		
		this.exhibition = null;
		
		this.provinceInfoList = provinceInfoManager.getAll();
		
		return SUCCESS;
	}
	
	/**
	 *@Todo:保存
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 下午02:31:08
	 * @throws IOException 
	 */
	public String save() throws Exception{
		
		if(exhibition != null){
			
			UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态
			exhibition.setUploadState(uploadState);
			
			if(Constants.Auto_Audit){
				AuditState auditState = auditStateManager.findUniqueBy("english_name",Constants.Audit_State_Pass);
				exhibition.setAuditState(auditState);
			}else{
				AuditState auditState = auditStateManager.findUniqueBy("english_name",Constants.Audit_State_Init);
				exhibition.setAuditState(auditState);
			}
			
			User userInfo = (User) session.getAttribute(Constants.Userinfo);
			exhibition.setEntryPerson(userInfo);
			exhibition.setModifyTime(Calendar.getInstance().getTime());
			exhibition.setUploadTime(Calendar.getInstance().getTime());
			
			if(exhibition.getProvinceInfo() == null || exhibition.getProvinceInfo().getId()== null){
				exhibition.setProvinceInfo(null);
			}
			if(exhibition.getCityInfo() == null || exhibition.getCityInfo().getId() == null ){
				exhibition.setCityInfo(null);
			}
			
			exhibitionManager.save(exhibition);
			generateImageOfExhibitionAfterSaved(exhibition);//第一次生成图片
			
			LogInfo logInfo = buildLogInfo(LogTypeEnum.addExhibition, "保存展览"+exhibition.getTitle()+"基本资料成功");
			logInfoManager.writeLog(logInfo);
		}
		return SUCCESS;
	}
	
	/**
	 *@Todo:列出展览
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 上午09:32:14
	 * @return
	 */
	public String list(){
		
		Criteria criteria;
		  
		this.auditStateList = auditStateManager.getAll();

	        if (sort != null) {
	            boolean isAsc = dir.equalsIgnoreCase("asc");
	            criteria = exhibitionManager.createCriteria(sort, isAsc);
	        } else {
	            criteria = exhibitionManager.createCriteria();
	        }
	        criteria.addOrder(Order.desc("id"));
	        
//	        UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态，是否锁定，隐藏等
//	        criteria = criteria.add(Restrictions.eq("uploadState", uploadState));
	        
	        User userInfo = (User) session.getAttribute(Constants.Userinfo);
	        if(userInfo!= null && "0".equals(userInfo.getAdministratorFlag())){
	        	 criteria = criteria.add(Restrictions.eq("entryPerson", userInfo));
	        }
	        
	    	if(auditStateType != null && !"all".equals(auditStateType)){
				 Long auditStateId = Long.valueOf(auditStateType);
				 AuditState auditState = auditStateManager.get(auditStateId);
				 criteria = criteria.add(Restrictions.eq("auditState", auditState));
			}
	        
	        if(startDate != null){
	        	 criteria = criteria.add(Restrictions.ge("uploadTime", startDate));
	        }
	        if(endDate != null){
	        	 criteria = criteria.add(Restrictions.lt("uploadTime", endDate));
	        }

	        if ((filterTxt != null) && (filterValue != null)
	                && (!filterTxt.equals("")) && (!filterValue.equals(""))) {
	            criteria = criteria.add(Restrictions.like(filterTxt,
	                        "%" + filterValue + "%"));
	        }

	        this.page = exhibitionManager.pagedQuery(criteria, curPage, pageSize);
	        
	        return SUCCESS;
	}
	
	/**
	 *@Todo:展览详情
	 *@CreateTime:2011-6-9 上午09:46:18
	 */
	public String modifyPrepare(){
		
		this.provinceInfoList = provinceInfoManager.getAll();
		
		if(exhibition != null){
			Long id = exhibition.getId();
			this.exhibition = exhibitionManager.get(id);
		}
		return SUCCESS;
	}
	
	/**
	 *@Todo:显示展览详情
	 *@author:zhuqh
	 *@CreateTime:2011-8-3 下午02:05:59
	 */
	public String showDetail(){
		
		this.provinceInfoList = provinceInfoManager.getAll();
		
		if(exhibition != null){
			Long id = exhibition.getId();
			this.exhibition = exhibitionManager.get(id);
		}
		
		return "showDetail";
	}
	
	/**
	 * 修改展览信息
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 上午10:19:01
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String modify() throws Exception{
		
		if(exhibition != null){
			
			UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init); //上传状态
			exhibition.setUploadState(uploadState);
			
			if(Constants.Auto_Audit){
				AuditState auditState = auditStateManager.findUniqueBy("english_name",Constants.Audit_State_Pass);
				exhibition.setAuditState(auditState);
			}else{
				AuditState auditState = auditStateManager.findUniqueBy("english_name",Constants.Audit_State_Init);
				exhibition.setAuditState(auditState);
			}
			
			User userInfo = (User) session.getAttribute(Constants.Userinfo);
			exhibition.setEntryPerson(userInfo);
			exhibition.setModifyTime(Calendar.getInstance().getTime());
			exhibition.setUploadTime(Calendar.getInstance().getTime());
			
			if(exhibition.getProvinceInfo() == null || exhibition.getProvinceInfo().getId()== null){
				exhibition.setProvinceInfo(null);
			}
			if(exhibition.getCityInfo() == null || exhibition.getCityInfo().getId() == null ){
				exhibition.setCityInfo(null);
			}
			
			Long exhibition_id = exhibition.getId();
			List list = exhibitionManager.findPaintingsByExhibitionId(exhibition_id);
			exhibition.setPaintings(list);
			
			exhibitionManager.save(exhibition);
			generateImageOfExhibitionAfterSaved(exhibition);//第一次生成图片
			
			LogInfo logInfo = buildLogInfo(LogTypeEnum.modifyExhibition, "修改展览"+exhibition.getTitle()+"基本资料成功");
			logInfoManager.writeLog(logInfo);
			
			request.setAttribute("ajaxData", "0000");
		}
		
		return SUCCESS;
	}
	
	/**
	 *@Todo:修改作品上传状态
	 *@author:zhuqh
	 *@CreateTime:2011-6-9 上午10:19:17
	 */
	public String changeUploadState(){
		
		if(exhibition != null){
			Long id = exhibition.getId();
			this.exhibition = exhibitionManager.get(id);
			
			if("1".equals(operateStr)){ //隐藏
				UploadState  uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Hidden);
				exhibition.setUploadState(uploadState);
				exhibition.setModifyTime(Calendar.getInstance().getTime());
				exhibitionManager.save(exhibition);
				
				LogInfo logInfo = buildLogInfo(LogTypeEnum.modifyExhibition, "修改展览"+exhibition.getTitle()+"基本信息,operate：隐藏");
				logInfoManager.writeLog(logInfo);
				
			}else if("2".equals(operateStr)){ // 删除
				
				exhibitionManager.removeById(id);
				
				LogInfo logInfo = buildLogInfo(LogTypeEnum.deleteExhibition, "删除展览"+exhibition.getTitle()+"基本信息");
				logInfoManager.writeLog(logInfo);
			}
			else if("3".equals(operateStr)){
				AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Pass);  //审核通过
				exhibition.setAuditState(auditState);
				exhibition.setModifyTime(Calendar.getInstance().getTime());
				exhibitionManager.save(exhibition);
				
				LogInfo logInfo = buildLogInfo(LogTypeEnum.audit_pass_exhibition, "审核通过展览"+exhibition.getTitle()+"基本信息");
				logInfoManager.writeLog(logInfo);
			}
			else if("4".equals(operateStr)){
				AuditState auditState = auditStateManager.findUniqueBy("english_name", Constants.Audit_State_Overrule);  //审核驳回
				exhibition.setAuditState(auditState);
				exhibition.setModifyTime(Calendar.getInstance().getTime());
				exhibitionManager.save(exhibition);
				
				LogInfo logInfo = buildLogInfo(LogTypeEnum.audit_overrule_exhibition, "审核驳回展览"+exhibition.getTitle()+"基本信息");
				logInfoManager.writeLog(logInfo);
			}
			else if("5".equals(operateStr)){//显示
				UploadState uploadState = uploadStateManager.findUniqueBy("english_name", Constants.Upload_State_Init);
				exhibition.setUploadState(uploadState);
				exhibition.setModifyTime(Calendar.getInstance().getTime());
				exhibitionManager.save(exhibition);
				
				LogInfo logInfo = buildLogInfo(LogTypeEnum.modifyExhibition, "修改展览"+exhibition.getTitle()+"基本信息,operate：显示");
				logInfoManager.writeLog(logInfo);
			}
		}
		
		request.setAttribute("ajaxData", "0000");
		
		return SUCCESS;
	}
	
	private void generateImageOfExhibitionAfterSaved(Exhibition exhibition) throws Exception{
		if( exhibition.getImageInfo() != null){
			ImageInfo img = imageInfoManager.get(exhibition.getImageInfo().getId());
			writeImg(img, Constants.ExhibitionFlag);
		}
	}
	
	public String getOperateStr() {
		return operateStr;
	}
	
	public void setOperateStr(String operateStr) {
		this.operateStr = operateStr;
	}
	
	public Exhibition getExhibition() {
		return exhibition;
	}
	
	public void setExhibition(Exhibition exhibition) {
		this.exhibition = exhibition;
	}
	
	public void setExhibitionManager(ExhibitionManager exhibitionManager) {
		this.exhibitionManager = exhibitionManager;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	public void setUploadStateManager(UploadStateManager uploadStateManager) {
		this.uploadStateManager = uploadStateManager;
	}
	
	public void setAuditStateManager(AuditStateManager auditStateManager) {
		this.auditStateManager = auditStateManager;
	}

	public String getAuditStateType() {
		return auditStateType;
	}

	public void setAuditStateType(String auditStateType) {
		this.auditStateType = auditStateType;
	}

	public List<AuditState> getAuditStateList() {
		return auditStateList;
	}

	public void setAuditStateList(List<AuditState> auditStateList) {
		this.auditStateList = auditStateList;
	}

	public List<ProvinceInfo> getProvinceInfoList() {
		return provinceInfoList;
	}

	public void setProvinceInfoList(List<ProvinceInfo> provinceInfoList) {
		this.provinceInfoList = provinceInfoList;
	}

	public void setProvinceInfoManager(ProvinceInfoManager provinceInfoManager) {
		this.provinceInfoManager = provinceInfoManager;
	}

	public void setLogInfoManager(LogInfoManager logInfoManager) {
		this.logInfoManager = logInfoManager;
	}
	
	public void setImageInfoManager(ImageInfoManager imageInfoManager) {
		this.imageInfoManager = imageInfoManager;
	}
	
}
